package com.lee.pullrefresh;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.lee.pullrefresh.adapter.TestDataAdapter;
import com.lee.pullrefresh.http.HttpTaskRunner.OnEventListener;
import com.lee.pullrefresh.http.test.GetTestData;
import com.lee.pullrefresh.http.test.TestData;
import com.lee.pullrefresh.ui.PullToRefreshBase;
import com.lee.pullrefresh.ui.PullToRefreshBase.OnRefreshListener;
import com.lee.pullrefresh.ui.PullToRefreshListView;

public class PullRefreshTestDataActivity extends Activity {
	
    private PullToRefreshListView mPullListView;
    private ListView mListView;
    private int page = 0;
    private boolean mIsStart = true;
    private int mCurIndex = 0;
   
    private List<GetTestData> mGetTestDatas = new ArrayList<GetTestData>();
    private TestDataAdapter mTestDataAdapter ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listview);
		mPullListView = (PullToRefreshListView) findViewById(R.id.pullrefreshview);
		//mListView = (ListView) findViewById(R.id.listview);
		mPullListView.setPullLoadEnabled(false);
		mPullListView.setScrollLoadEnabled(true);
		
		
		testData();
		mTestDataAdapter = new TestDataAdapter(this);
		mListView = mPullListView.getRefreshableView();
		mListView.setAdapter(mTestDataAdapter);
		
		 
		mPullListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			    mIsStart = true;
                new GetDataTask().execute();
               Toast.makeText(getApplicationContext(), "下拉刷新", 1).show();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mIsStart = false;
                new GetDataTask().execute();
			}
		});
		   setLastUpdateTime();
	   //     mPullListView.doPullRefreshing(true, 500);
	}
	
	TestData mTestData;
	private void testData(){
		mTestData = new TestData();
		mTestData.uid =1;
		mTestData.page = page;
		mTestData.setOnEventListener(new OnEventListener() {
			
			@Override
			public void onFinished(Message msg) {
				if (mTestData.responseSuccess()) {
					if (mTestData.mResult == 100) {
						Log.d("msg", "测试数据 ： "+mTestData.resultData);
						mGetTestDatas = GetTestData.getMyOrderLists(mTestData.resultData);
						Toast.makeText(getApplicationContext(), mGetTestDatas.size()+"", 1).show();
						
						for (GetTestData mGetTestData : mGetTestDatas) {
							mTestDataAdapter.addNoNotifyUI(mGetTestData);
							mTestDataAdapter.notifyDataSetChanged();
						}
					}
				}else {
					Toast.makeText(PullRefreshTestDataActivity.this, mTestData.mErrorInfo, 1).show();
				}
			}
		});
		try {
			mTestData.execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 private class GetDataTask extends AsyncTask<Void, Void, List<GetTestData>> {

	        @Override
	        protected List<GetTestData> doInBackground(Void... params) {
	            // Simulates a background job.
	            try {
	                Thread.sleep(3000);
	            } catch (InterruptedException e) {
	            }
	            return mGetTestDatas;
	        }

	        @Override
	        protected void onPostExecute(List<GetTestData> result) {
	            boolean hasMoreData = true;
	            if (mIsStart) {
	            } else {
	            	page = page+15;
	            	testData();
	            	if (mGetTestDatas.size() < 15) {
	            		hasMoreData = false;
					}
	            }
	            
	            mTestDataAdapter.notifyDataSetChanged();
	            mPullListView.onPullDownRefreshComplete();
	            mPullListView.onPullUpRefreshComplete();
	            mPullListView.setHasMoreData(hasMoreData);
	            setLastUpdateTime();

	            super.onPostExecute(result);
	        }
	    }
	 
	 private void setLastUpdateTime() {
	        String text = formatDateTime(System.currentTimeMillis());
	        mPullListView.setLastUpdatedLabel(text);
	    }
	 
	  private String formatDateTime(long time) {
	        if (0 == time) {
	            return "";
	        }
	        
	        return mDateFormat.format(new Date(time));
	    }
	  
	  private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
}
